package com.houspace.utils;

import com.alibaba.fastjson.JSONObject;
import com.houspace.utils.smexception.CodedBaseRuntimeException;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

public class LocationRevertUtils {

    private static String apiUrl = "http://apis.map.qq.com/ws/geocoder/v1/?location=%s&key=%s";
    private static String apiKey = "WDZBZ-3YCW5-4NKIS-QW46V-WFEBK-4WB7J";

    public static String parse(String latitude, String longitude){
        return parse(StringUtils.join(latitude, ",", longitude));
    }

    public static String parse(String latitudeAndLongitude){
        try {
            return HttpClientUtils.doGet(String.format(apiUrl, latitudeAndLongitude, apiKey));
        } catch (IOException e) {
            throw new CodedBaseRuntimeException("parse latitude & longitude " + latitudeAndLongitude + " failed", e);
        }
    }

    public static void main(String[] args) {

        String result= parse("30.27283,120.1302");
        JSONObject object = JSONObject.parseObject(result);
        JSONObject locationObject = object.getJSONObject("result");
        JSONObject ad_info = locationObject.getJSONObject("ad_info");
        System.out.println(ad_info.get("location"));
        ad_info.put("location", ad_info.getJSONObject("location"));
    }
}
